<?php
#+------------------------------------------------------------------
#| 普通的。
#+------------------------------------------------------------------
#| Author:Janmas Cromwell <janmas-cromwell@outlook.com>
#+------------------------------------------------------------------
namespace app\library\websocket\logic;

use app\library\redis\Redis;
use uuid\UUid;

/**
 * 会话逻辑
 */
class Sessions
{

	const PREFIX = 'session_';
	const MSG    = 'session_msg_';

	/**
	 * 往会话里添加一条消息
	 * @param string $sessionId
	 * @param string $msgId
	 * @return bool|int
	 */
	static function add(string $sessionId, string $msgId)
	{
		$redis = Redis::get();
		$res = $redis->sAdd(self::MSG . $sessionId, $msgId);
		$redis->hSet($sessionId, 'last_time', Messages::get($msgId, 'timestamp'));
		$redis->hSet($sessionId, 'last_message', Messages::get($msgId, 'data'));
		Redis::put($redis);
		return $res;
	}

	/**
	 * 从会话消息记录删除一条消息
	 * @param string $sessionId
	 * @param string $msgId
	 * @return int
	 */
	static function remove(string $sessionId, string $userId)
	{
		$redis = Redis::get();
		if ($userId == $redis->hGet($sessionId, 'user_id')) {
			$res = $redis->hSet($sessionId, 'user_del', 1);
		} else {
			$res = $redis->hSet($sessionId, 'to_del', 1);

		}
		Redis::put($redis);
		return $res;
	}

	/**
	 * 获取消息id集合
	 * @param string $sessionId
	 * @return array
	 */
	static function getMsgIdList(string $sessionId)
	{
		$redis = Redis::get();
		$res = $redis->sMembers(self::MSG . $sessionId);
		Redis::put($redis);
		return $res;
	}

	/**
	 * 获取消息列表
	 * @param string $sessionId
	 * @return array
	 */
	static function getMsgList(string $sessionId)
	{
		$msgIdList = self::getMsgIdList($sessionId);
		$list = [];
		foreach ($msgIdList as $msgId) {
			$list[] = Messages::get($msgId);
		}

		return $list;
	}

	/**
	 * 创建会话
	 * @param string $userId
	 * @param string $toUid
	 */
	static function create(string $userId, string $toUid)
	{
		$redis = Redis::get();
		$chatId = UUid::create();
		$redis->multi();
		$redis->hSet($chatId, 'client', $userId);
		$redis->hSet($chatId, 'touid', $toUid);
		$redis->hSet($chatId, 'last_time', time());
		$redis->hSet($chatId, 'last_message', '');
		$redis->hSet($chatId, 'user_del', 0);
		$redis->hSet($chatId, 'to_del', 0);
		$redis->hSet($chatId, 'group_id', 0);
		$redis->hSet($chatId, 'user_read', 1);
		$redis->hSet($chatId, 'to_read', 1);
		$redis->sAdd(self::PREFIX . $userId, $chatId);
		$redis->sAdd(self::PREFIX . $toUid, $chatId);
		$res = $redis->exec();
		Redis::put($redis);
		return $res;
	}

	/**
	 * 获取指定用户的会话列表
	 * @param string $userId
	 * @return array
	 */
	static function getUsersSessionList(string $userId)
	{
		$redis = Redis::get();
		$sessionIdList = $redis->sMembers(self::PREFIX . $userId);
		$sessionList = [];
		foreach ($sessionIdList as $sessionId) {
			$sessionList[] = self::getSessionInfo($sessionId);
		}
		Redis::put($redis);
		return $sessionList;
	}

	/**
	 * 获取指定的会话信息
	 * @param string $sessionId
	 * @return array
	 */
	static function getSessionInfo(string $sessionId, string $field = '*')
	{
		$redis = Redis::get();
		if ($field == '*') {
			$info = $redis->hGetAll($sessionId);
			$info['receiver'] = Users::getUserById($info['touid']);
            $info['sender'] = Users::getUserById($info['client']);
            $info['timestamp'] = (new \timestamp\TimeStamp((int)$info['last_time']))->friendly();
		} else {
			$info = $redis->hGet($sessionId, $field);
		}
		Redis::put($redis);
		return $info;
	}

	static function read(string $sessionId, string $userId)
	{
        $redis = Redis::get();
        if ($userId == self::getSessionInfo($sessionId, 'client')) {
            $redis->hSet($sessionId, 'user_read', 1);
        } else {
            $redis->hSet($sessionId, 'to_read', 1);
        }
        Redis::put($redis);
        return true;
    }
}
